var prefix = "/biz/software"

$().ready(function() {
	validateRule();
});
$(function() {
	load();
	var ctrl=document.getElementById("ssn");
	ctrl.focus();
});
function load(){
	var myDate = new Date();

	year=myDate.getFullYear();    //获取完整的年份(4位,1970-????)
	month=myDate.getMonth()+1;       //获取当前月份(0-11,0代表1月)
	day=myDate.getDate();        //获取当前日(1-31)
	var currDate=year+'-'+month+'-'+day;
	var submitDate=document.getElementById("submitDate").value;
	if(submitDate==""){	document.getElementById("submitDate").value=currDate;
	}
}
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
		function formatDate(d,format) {
			// 如果date不是日期对象，返回
			if(!(d instanceof Date)) {
				return;
			}
			var year = d.getFullYear();    // 获取年
			var month = d.getMonth() + 1 ; // 月份是从0开始
			var day = d.getDate();         // 获取日
			var hours = d.getHours();      // 获取时间 0~23
			var minutes = d.getMinutes();  // 获取分钟
			var seconds = d.getSeconds();  // 获取秒数
			// 如果小于10 数据要补充0
			// 写一个函数是 yyyy-MM-dd HH:mm:ss(优化后)
			month < 10 ? month = "0"+month: month;
			day < 10 ? day = "0"+day:day;
			hours < 10 ? hours = "0"+ hours: hours;
			minutes < 10 ? minutes = "0"+ minutes: minutes;
			seconds < 10 ? seconds = "0"+ seconds: seconds;
			if(format==0){return year+"-"+month+"-"+day;}
			if(format==1){return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;}


		}


function update() {
	suppReason=document.getElementById("suppReason").value;
	historyReason=document.getElementById("historyReason").value;
	document.getElementById("suppReason").value=suppReason+"\n"+historyReason;
	var day1 = new Date();
	today= formatDate(day1,0);

	if(today==$('#submitDate').val()){
		today=formatDate(day1,1);
		$('#submitDate').val(today);
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : "/biz/software/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("提交成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function save() {
	suppReason=document.getElementById("suppReason").value;
	historyReason=document.getElementById("historyReason").value;
	document.getElementById("suppReason").value=suppReason+"\n"+historyReason;
	var day1 = new Date();
	today= formatDate(day1,0);

	if(today==$('#submitDate').val()){
		today=formatDate(day1,1);
		$('#submitDate').val(today);
	}
    $('#status').val(0);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/biz/software/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("保存成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function reload() {
	var sn=document.getElementById('ssn').value;
	var approvalDate=document.getElementById('approvalDate').value;

	sn=sn+','+approvalDate;
	var index=parent.layer.open({
		type: 2,
		title: '编辑',
		maxmin: true,
		shadeClose: false,
		area: ['800px', '520px'],
		content: prefix + '/reload/' + sn
	});
	parent.layer.full(index);

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			sn : {required : true},
			copyrightOwner : {required : true},
			softwareName : {required : true},
			businessMan : {min : 0},
			approvalDate : {required : true},
			type : {required : true},
			agentCompany : {
				required : true,
				minlength : 1}
		},
		messages : {
			sn             : {required : icon + "请输入流水号"},
			copyrightOwner : {required : icon + "请输入著作权人"},
			softwareName   : {required : icon + "请输入软件名称"},
			businessMan    : {min : icon + "请选择业务员"},
			approvalDate   : {required : icon + "请输入发证日期"},
			type           : {required : icon + "请选择是否写材料"},
			agentCompany   : {
				required : icon + "请输入代理公司",
				minlength : icon + "请输入代理公司"
			}
		}
	})
}